<?php

namespace backend\models;

use Yii;

/**
 * This is the model class for table "mall_material".
 *
 * @property int $id
 * @property int $pid 一级分类
 * @property int $mid 菜单分类
 * @property string $title 标题
 * @property int $style 1单图 2多图 3视频
 * @property string $words 文字
 * @property string $img_url 图片或者图片集
 * @property string $video_url 视频url
 * @property int $down_count 下载次数
 * @property int $sort 排序
 * @property int $update_time 修改时间
 * @property int $create_time 创建时间
 * @property int $status -1禁用 1启用
 * @property int $publisher_id 素材发布者
 */
class MallMaterial extends \yii\db\ActiveRecord
{

    //定义场景
    const SCENARIO_UPLOAD = 'upload';
    const SCENARIO_VIDEO = 'video';
    const SCENARIO_DEFAULT = 'default';

    public $file;

    public $file_video;
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'mall_material';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['pid', 'mid', 'style', 'down_count', 'sort', 'update_time', 'create_time', 'status', 'publisher_id'], 'integer'],
            [['mid', 'pid', 'style', 'words', 'publisher_id'], 'required'],
            [['img_url', 'video_url'], 'string'],
            [['file', 'file_video'],'file'],
            [['title'], 'string', 'max' => 20],
            [['words'], 'string', 'max' => 65535],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'pid' => '一级分类',
            'mid' => '二级分类',
            'title' => '标题',
            'style' => '素材类型',
            'words' => '文字',
            'img_url' => '图片',
            'video_url' => '视频',
            'down_count' => '下载次数',
            'sort' => '排序',
            'update_time' => '修改时间',
            'create_time' => '创建时间',
            'publisher_id' => '素材发布者',
            'status' => '状态',
        ];
    }

    public static function attributeValue()
    {
        return [
            'status' => [
                '1' => '启用',
                '-1' => '禁用'
            ],
            'style' => [
                '1' => '文字',
                '2' => '单图',
                '3' => '多图',
                '4' => '视频',
            ],
        ];
    }

    public static function getMenu()
    {
        $sql = "SELECT id, title FROM mall_material_menu WHERE rank = 1";
        $result =  Yii::$app->db->createCommand($sql)->queryAll();
        $select = [];
        foreach ($result as $key => $value) {
            $select[$value['id']] = $value['title'];
        }
        return $select;
    }

    public function getStatus()
    {
        switch($this->status)
        {
            case -1:
                $value = '禁用';
                break;
            case 1:
                $value = '启用';
                break;
            default:
                $value = '未知';
                break;
        }
        return $value;
    }

    public function getEnuMenu()
    {
        $menu = MallMaterialMenu::findOne($this->pid);
        if ($menu) {
            return $menu->title;
        } else {
            return '';
        }
    }

    public function getStyle()
    {
        switch($this->style)
        {
            case 1:
                $value = '文字';
                break;
            case 2:
                $value = '单图';
                break;
            case 3:
                $value = '多图';
                break;
            case 4:
                $value = '视频';
                break;
            default:
                $value = '未知';
                break;
        }
        return $value;
    }

    public function getCmfMaterialMenu()
    {
        return $this->hasOne(MallMaterialMenu::class, ['id' => 'mid']);
    }

    public function getCmfMaterialMenus()
    {
        return $this->hasOne(MallMaterialMenu::class, ['id' => 'pid']);
    }

    public function scenarios()
    {
        $scenarios = parent::scenarios();
        $scenarios[self::SCENARIO_UPLOAD] = ['file'];
        $scenarios[self::SCENARIO_VIDEO] = ['file_video'];
        $scenarios[self::SCENARIO_DEFAULT] = ['mid','pid', 'publisher_id', 'title', 'style', 'words', 'img_url', 'video_url', 'down_count', 'sort', 'update_time', 'create_time', 'status'];
        return $scenarios;
    }

    /**
     * 通过二级菜单获取一级菜单
     * @param $mid
     * @return array|false
     */
    public static function getFirstMenu($mid)
    {
        $sql = "SELECT f.id, f.title FROM mall_material_menu AS F LEFT JOIN mall_material_menu AS S ON f.id = s.parent_id WHERE s.id = {$mid}";
        return Yii::$app->db->createCommand($sql)->queryOne();
    }
}
